const shopModel = require('../db/shoplist')
const {resinfo} = require('../utils/utils')
module.exports = {
    //获取所有商品数据
    getshopData:async (req,res)=>{
        console.log(res,req);
        // let {page} = req.params
        let shoplist = await shopModel.find();
        // console.log(shoplist);
        let resobj = {
            Code:0,
            mag:'数据获取成功',
            data:shoplist
        }
        //判断是否查询导数据
        if(shoplist.length){
            res.json(resobj)
        }else{
            resobj.Code = 1
            resobj.mag = '数据获取失败'
            resobj.data = null
            res.json(resobj)
        }
    },
    //获取某一个商品的数据
    getDetail:async (req,res)=>{
        console.log(req.params);
        let {id} = req.params
        console.log(111,id);
        let shopsDetail
        try {
            shopsDetail = await shopModel.find({_id:id})
            console.log(123,shopsDetail);
        } catch (error) {
            console.log(error);
            resinfo(res,2,'请求错误，请刷新后重试')
            return
        }
        if(shopsDetail.length){
            resinfo(res,0,'数据获取成功',{data:shopsDetail[0]})
        }else{
            resinfo(res,1,'数据获取失败')
        }
    },
    //后台删除藏品数据
    removeAdmin: async(req,res)=>{
        let {shopId} = req.body
        try {
           await shopModel.deleteOne({_id:shopId});
           resinfo(res,0,'删除成功')
        } catch (error) {
            resinfo(res,1,'删除失败，缺失参数')
        }
    },
    //后台增加藏品数据
    addAdmin: async (req,res)=>{
        let {shopName,shopPrice} = req.body
        let addResult = await shopModel.insertMany({
            shopPrice,
            shopName
        })
        if(addResult.length){
            resinfo(res,0,'添加成功')
        }else{
            resinfo(res,1,'添加失败，缺失参数')
        }
    },
    //后台修改藏品数据
    updateAdmin: async(req,res)=>{
        let {id} = req.params
        let {shopName,shopPrice} = req.body
        try {
            await shopModel.updateOne({_id:id},{$set:{
                shopName,
                shopPrice,
            }})
            resinfo(res,0,'修改成功')
        } catch (err) {
            resinfo(res,1,'修改失败，缺失参数')
        }
    }
    
}